package com.ht.api.request.wwx;

import cn.hutool.json.JSONObject;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;
import com.ht.api.db.annotate.*;
import com.ht.api.db.bean.DataResult;
import com.ht.api.db.bean.SessionBean;
import com.ht.api.db.enums.Param;
import com.ht.api.db.request.IHandleRequest;
import com.ht.api.db.util.HtUtil;
import com.ht.api.util.CallTestUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@FuncId("1914499631505608704")
@FuncGroup("企业微信")

@FuncQuery("wx_groupchat_customer")
@FuncName("社群客户查询")
public class GroupChatCustomerQuery implements IHandleRequest {

	@Override
	public void after(SessionBean me, JSONObject param, DataResult result) {
		DbList list = result.getRecords();

		for (Entity customer : list) {
			String openid = customer.getStr("external_userid");

			if (customer.getInt("type") == 1) {
				String sql = "select oneid,nickname,aliasname,avatar,gender from user_main where status = 0 and bitmap_contains(users, "+Math.abs(openid.hashCode())+")";

				Entity user = Db.findOne(me.getDataSource(), sql);
				customer.set("mapper_oneid", user);
			} else {
				Entity touch = Db.createSqlParser("wx_workuser").select("alias,avatar,name,userid").where("userid", openid).findOne(me.getDataSource());
				customer.set("touch_workuser", touch);
			}
		}
	}

	public static void main(String[] args) {

		JSONObject result = CallTestUtil.autoTest(new Object(){}.getClass().getEnclosingClass().getSimpleName());
		System.err.println(result.toStringPretty());
	}
}
